// pages/timely/timely.js
var sliderWidth = 96;
Page({
  addToCart(e){
    console.log(e.currentTarget.dataset)
    wx.setStorage({
      key: 'id',
      data: e.currentTarget.dataset,
      success: function(res) {console.log('success')},
      fail: function(res) {},
      complete: function(res) {},
    })
  },
  active(e) {
    let currId = e.currentTarget.id
    this.setData({
      currId
    })
    this.calcShowgoods()
  },

  /**
   * 页面的初始数据
   */
  getData() {
    wx.showLoading({
      title: 'loading',
      mask: true,
      success: function(res) {},
      fail: function(res) {},
      complete: function(res) {},
    })
    wx.request({
      url: 'http://rap2api.taobao.org/app/mock/119921/api/v1/classify',
      data: '',
      header: {},
      method: 'GET',
      dataType: 'json',
      responseType: 'text',
      success: (res) => {
        this.setData({
          classify: res.data.data
        })
        wx.showToast({
          title: 'success'
        })
      },
      fail: function(res) {},
      complete: function(res) {
        wx.hideLoading()
      },
    })
  },
  pageSize: 10,
  data: {
    classify: [],
    goods: [],
    showgoods: [],
    currId: 1
  },
 
  getGoods() {
    wx.showLoading({
      title: 'loading',
      mask: true,
      success: function(res) {},
      fail: function(res) {},
      complete: function(res) {},
    })
    wx.request({
      url: 'http://rap2api.taobao.org/app/mock/119921/api/v1/goods',
      data: '',
      header: {},
      method: 'GET',
      dataType: 'json',
      responseType: 'text',
      success: (res) => {
        this.setData({
          goods: res.data.data
        },() => {
          this.calcShowgoods()
        })

        wx.showToast({
          title: 'success'
        })
      },
      fail: function(res) {},
      complete: function(res) {
        wx.hideLoading()
      },
    })
  },
  calcShowgoods() {
    const start = (this.data.currId - 1) * this.pageSize;
    const end = start + this.pageSize
    const showgoods = this.data.goods.slice(start, end)
    this.setData({
      showgoods
    },() => {

    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.getData();
    this.getGoods();
    var that = this;
    // wx.getSystemInfo({
    //   success (res) {
    //     console.log(res)
    //   }
    // });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})